package com.jiuyou.clientController.order;

import com.alibaba.fastjson.JSONObject;
import com.jiuyou.clientController.base.ClientBaseController;
import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.CopyUtils;
import com.ruoyi.order.domain.OrderDo;
import com.ruoyi.order.domain.StoreProductOrder;
import com.ruoyi.order.service.IStoreProductOrderService;
import com.ruoyi.product.domain.StoreProductVo;
import com.ruoyi.shopping.domain.MallOrder;
import com.ruoyi.shopping.service.IMallOrderService;
import com.ruoyi.store.domain.Store;
import com.ruoyi.store.service.IStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.DataInput;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: chengliang
 * @Date: Created in 2022/1/25
 */
@RestController
@RequestMapping("/client/order/list")
public class OrderListClientController  extends ClientBaseController {
    @Autowired
    private IStoreProductOrderService orderService;
    @Autowired
    private IStoreService storeService;
    @Autowired
    private IMallOrderService mallOrderService;
    @PostMapping("/{id}")
    public AjaxResult getOne(@PathVariable Long id){
        StoreProductOrder order = orderService.selectStoreProductOrderById(id);
        return AjaxResult.success(order);
    }

    @GetMapping
    public AjaxResult getList(String state){
        StoreProductOrder order = new StoreProductOrder();
        OrderDo orderDo = new OrderDo();
        if(!"all".equals(state)){
            switch (state){
                case "wait_pay":
                    orderDo.setOOrderState(DicConstants.ORDER_STATE_Wait_Pay.getCode());
                    orderDo.setOrderState(DicConstants.ORDER_STATE_Wait_Pay.getCode());
                    orderDo.setAftersaleState(DicConstants.SYS_N.getCode());
//                    order.setPayState(DicConstants.SYS_Y.getCode());
                    break;
                case "wait_use":
                    orderDo.setOrderState(DicConstants.ORDER_STATE_Wait_Use.getCode());
                    orderDo.setAftersaleState(DicConstants.SYS_N.getCode());
//                    order.setPayState(DicConstants.SYS_Y.getCode());
                    break;
                case "wait_comment":
//                    order.setCommentId(0L);
                    orderDo.setAftersaleState(DicConstants.SYS_N.getCode());
                    orderDo.setCommentState(DicConstants.SYS_N.getCode());
                    orderDo.setOrderState(DicConstants.ORDER_STATE_Received.getCode());
                    orderDo.setOOrderState(DicConstants.ORDER_STATE_Used.getCode());
//                    orderDo.set
                    break;
                case "refund_success":
                    orderDo.setAftersaleState(DicConstants.SYS_Y.getCode());
                    break;
                case "wait_ship":
                    orderDo.setShipState(DicConstants.ORDER_STATE_Wait_Ship.getCode());
//                    orderDo.setOrderState(DicConstants.ORDER_STATE_Shipped.getCode());
                    break;

            }
        }
        orderDo.setMemberId(getLoginMemberId());
        startPage();
//        List<StoreProductOrder> list = orderService.selectStoreProductOrderList(order);
        orderDo.setOrderType("order");
        List<OrderDo> list = orderService.selectOrderList(orderDo);
        List<HashMap<String, List<JSONObject>>> maps = new ArrayList<HashMap<String, List<JSONObject>>>();
        TableDataInfo tableDataInfo=getDataTable(list);
        list.forEach(e->{
            if(e.getType().equals("M")){
                List<MallOrder> mallOrders = mallOrderService.selectMallOrderByMeshId(e.getId());
                e.setMallOrder(mallOrders);
                e.setPayPrice(e.getPayPrice().setScale(2, BigDecimal.ROUND_DOWN));
            }
        });
//        List<Long> storeIds = list.stream().map(StoreProductOrder::getStoreId).distinct().collect(Collectors.toList());
//        List<Store> stores = storeService.selectStoreListByIds(storeIds);
//        List<StoreProductVo> vos = CopyUtils.copyList(list, StoreProductVo.class);
//        vos.forEach(e->{
//            stores.forEach(s->{
//                if(e.getStoreId() == s.getId()){
//                    e.set
//                }
//            });
//        });
        return AjaxResult.success(tableDataInfo);
    }
}
